package com.softer.chengxin.blacklist.service.iml;

import com.softer.chengxin.blacklist.dao.BlacklistDao;
import com.softer.chengxin.blacklist.domin.BlacklistVO;
import com.softer.chengxin.blacklist.service.BlacklistService;
import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.*;
import com.softer.chengxin.corp.dao.CorpDao;
import com.softer.chengxin.home.dao.MessageDao;
import com.softer.chengxin.home.domin.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangixnrui
 * @date 2018/10/12
 */
@Service
public class BlacklistServiceImpl implements BlacklistService {

    @Autowired
    private BlacklistDao blacklistDao;
    @Autowired
    private CorpDao corpDao;
    @Autowired
    private MessageDao messageDao;

    /**
     * 主管部门查询企业黑名单列表 条数
     *
     * @param page        页码
     * @param qyName      企业名
     * @param cxDateBegin 查询录入时间开始
     * @param cxDateEnd   查询录入时间结束
     * @param state       状态
     * @param loginName   登录名
     * @return 结果
     */
    @Override
    public ManagerPageResult load(Page page, String qyName, String cxDateBegin, String cxDateEnd, Integer state, String loginName) {
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        String area = null;
        if (!Common.ADMIN.equals(loginName)) {
            area = corpDao.getDistinct(loginName);
        }
        int count = blacklistDao.findBlacklistByConditionCount(qyName, cxDateBegin, cxDateEnd, state, area);
        if (count <= NumberEnum.ZERO.getNum()) {
            common(mpr,jr);
            return mpr;
        }
        List list = blacklistDao.findBlacklistByCondition(qyName, cxDateBegin, cxDateEnd, state, area, page);
        jr.setDs(list);
        mpr.setCount(count);
        mpr.setJson(jr);
        return mpr;
    }

    /**
     * 新增黑名单
     *
     * @param blacklist 实体
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult add(BlacklistVO blacklist, String loginName) {
        //查询该企业是否已经在黑名单中且生效
        int num = blacklistDao.findCorpInfo(blacklist.getCompanyId());
        if (num > NumberEnum.ZERO.getNum()) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.CORP_IS_BLACKLIST);
        }
        blacklist.setAdder(loginName);
        blacklist.setState(StateEnum.TACK_EFFECT.getCode());
        //添加黑名单
        int ret = blacklistDao.add(blacklist);
        if (ret > Integer.parseInt(Common.ZERO)) {
            //增加通知消息
            addMessage(blacklist, loginName);
            return new CommonResult(ResultEnum.SUCCESS.getCode(), Common.OPERATE_SUCCESS);
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), Common.OPERATE_FAIL);
    }

    /**
     * 移除黑名单
     *
     * @param blacklist 实体
     * @param loginName 用户名
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult remove(BlacklistVO blacklist, String loginName) {
        //谁添加的谁删除
        String adder = blacklistDao.getAdder(blacklist.getId());
        if (!loginName.equals(adder)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.NO_AUTHORITY);
        }
        blacklist.setAdder(loginName);
        blacklist.setState(StateEnum.LOSE_EFFICACY.getCode());
        //移除黑名单
        int ret = blacklistDao.remove(blacklist);
        if (ret > Integer.parseInt(Common.ZERO)) {
            //增加通知消息
            addMessage(blacklist, loginName);
            return new CommonResult(ResultEnum.SUCCESS.getCode(), Common.OPERATE_SUCCESS);
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), Common.OPERATE_FAIL);
    }

    /**
     * 获取黑名单详情
     *
     * @param id 黑名单id
     * @return 结果
     */
    @Override
    public BlacklistVO getBlackDetail(Integer id) {
        return blacklistDao.getBlackDetail(id);
    }

    public ManagerPageResult common(ManagerPageResult mpr, JsonResult jr) {
        jr.setDs(new ArrayList());
        mpr.setCount(NumberEnum.ZERO.getNum());
        mpr.setJson(jr);
        return mpr;
    }

    /**
     * 新增通知消息
     *
     * @param blacklistVO 实体
     * @param loginName   登陆用户
     */
    private void addMessage(BlacklistVO blacklistVO, String loginName) {
        Message message = new Message();
        message.setAdder(loginName);
        message.setCropid(blacklistVO.getCompanyId());
        //企业信息的消息类型定义为0
        message.setType(Integer.parseInt(Common.ZERO));
        message.setUrl(UrlEnum.COMPANY_INFO.getUrl());
        if (blacklistVO.getState() == StateEnum.TACK_EFFECT.getCode()) {
            message.setTitle(MessageEnum.IS_ADD_BLACKLIST.getMsg());
        } else {
            message.setTitle(MessageEnum.IS_REMOVE_BLACKLIST.getMsg());
        }
        messageDao.addMessage(message);
    }


}
